
package com.plian.system.service.sys.code.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plian.Tools.DateUtil;
import com.plian.Tools.Tools;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.entity.sys.FormCode;
import com.plian.system.mapper.sys.FormCodeMapper;
import com.plian.system.service.sys.code.IFormCodeService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

/**
 * 单据编码 服务实现类
 *
 * @author jianglei
 * @since 2019-10-23
 */
@Service
@AllArgsConstructor
public class FormCodeServiceImpl extends ServiceImpl<FormCodeMapper, FormCode> implements IFormCodeService {
    /**
     * logger属性.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(FormCodeServiceImpl.class);

    @Override
    public String getCode(int formType) {
        QueryWrapper<FormCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("form_type", formType);
        FormCode formCode = baseMapper.selectOne(queryWrapper);
        if (!Optional.ofNullable(formCode).isPresent()) {
            LOGGER.error("FormCodeServiceImpl getCode----->" + formType + " 单据类型未配置");
            return "";
        }
        Date now = DateUtil.now();
        int codeNumber = formCode.getCodeNumber();
        if (!DateUtil.isTheSameDay(now, formCode.getUpdateTime())) {
            codeNumber = 1;
        }

        formCode.setCodeNumber(codeNumber + 1);
        formCode.setUpdateTime(now);
        update(formCode, queryWrapper);

        return formCode.getFormName() + Tools.date2Str(now, "yyyyMMdd") + String.format("%04d", codeNumber);
    }

    @Override
    public String getFilingCode(int formType) {
        return getFilingCode(formType,false);
    }

    @Override
    public String getContainBracketsFilingCode(int formType) {
        return getFilingCode(formType, true);
    }

    public String getFilingCode(int formType, boolean isContainBrackets){
        QueryWrapper<FormCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("form_type", formType);
        Date now = DateUtil.now();
        FormCode formCode = baseMapper.selectOne(queryWrapper);
        if (!Optional.ofNullable(formCode).isPresent()) {
            LOGGER.error("FormCodeServiceImpl getApprovalCode----->" + formType + " 单据类型未配置");
            return "";
        }

        int filingCodeNumber = formCode.getFilingCodeNumber();
        if (!DateUtil.isTheSameYear(now, formCode.getFilingUpdateTime())) {
            filingCodeNumber = 1;
        }

        formCode.setFilingCodeNumber(filingCodeNumber + 1);
        formCode.setFilingUpdateTime(now);
        update(formCode, queryWrapper);
        String year = Tools.date2Str(now, "yyyy");
         if (isContainBrackets){
             year = String.format("[%s]", year);
        }
        return year + String.format("%03d", filingCodeNumber);
    }

    @Override
    public PageData getNoFormatFilingCode(int formType) {

        QueryWrapper<FormCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("form_type", formType);
        FormCode formCode = baseMapper.selectOne(queryWrapper);
        if (!Optional.ofNullable(formCode).isPresent()) {
            LOGGER.error("FormCodeServiceImpl getApprovalCode----->" + formType + " 单据类型未配置");
            return null;
        }
        Date now = DateUtil.now();
        int filingCodeNumber = formCode.getFilingCodeNumber();
        if (!DateUtil.isTheSameYear(now, formCode.getFilingUpdateTime())) {
            filingCodeNumber = 1;
        }

        formCode.setFilingCodeNumber(filingCodeNumber + 1);
        formCode.setFilingUpdateTime(now);
        update(formCode, queryWrapper);

        PageData pageData = new PageData();
        pageData.put("year", Tools.date2Str(now, "yyyy"));
        pageData.put("codeNumber", filingCodeNumber);
        return pageData;
    }

    @Override
    public String getCodeName(int formType) {
        QueryWrapper<FormCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("form_type", formType);
        FormCode formCode = baseMapper.selectOne(queryWrapper);
        if (!Optional.ofNullable(formCode).isPresent()) {
            LOGGER.error("FormCodeServiceImpl getCode----->" + formType + " 单据类型未配置");
            return "";
        }
        Date now = DateUtil.now();
        int codeNumber = formCode.getCodeNumber();
        if (!DateUtil.isTheSameDay(now, formCode.getUpdateTime())) {
            codeNumber = 1;
        }

        formCode.setCodeNumber(codeNumber + 1);
        formCode.setUpdateTime(now);
        update(formCode, queryWrapper);

        return formCode.getCodeName() + "_" + Tools.date2Str(now, "yyyyMMdd") + "_" + String.format("%05d", codeNumber);
    }

    @Override
    public String getRecordCodeName(int formType) {
        QueryWrapper<FormCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("form_type", formType);
        FormCode formCode = baseMapper.selectOne(queryWrapper);
        if (!Optional.ofNullable(formCode).isPresent()) {
            LOGGER.error("FormCodeServiceImpl getCode----->" + formType + " 单据类型未配置");
            return "";
        }
        Date now = DateUtil.now();
        int codeNumber = formCode.getCodeNumber();
        if (!DateUtil.isTheSameDay(now, formCode.getUpdateTime())) {
            codeNumber = 1;
        }

        formCode.setCodeNumber(codeNumber + 1);
        formCode.setUpdateTime(now);
        update(formCode, queryWrapper);

        return formCode.getCodeName() + "(" + Tools.date2Str(now, "yyyy") + ")" + String.format("%02d", codeNumber);
    }

}
